Easy plots with ggplot2

Using curly curly brackets in functions (tidyeval)

lruolin
10-04-2021

Background

I was watching Julia Silge’s youtube video on tidymodels, and saw that she had a very neat way of creating visualizations for exploratory data analysis.

A function is created to reduce the copying and pasting of code, and you can use curly curly brackets to refer to variables directly.

Let’s try it out!

Packages

Data

mtcars_df <- mtcars %>% 
  as.data.frame()

An easy plot

ggplot(aes(mpg, cyl), data = mtcars_df) +
  geom_point() +
  labs(title = "Plot of cyl against mpg") +
  theme_classic() +
  theme(axis.text = element_text(size = 14),
        axis.title = element_text(face = "bold", size = 16),
        title = element_text(face = "bold", size = 20))

Writing a function

# function
 
# var_x : x variable
# var_y : y variable
# title : title of chart, in string

plot_mtcars <- function(var_x, var_y, title) {

  ggplot(aes({{var_x}}, {{var_y}}), data = mtcars_df) +
  geom_point() +
  labs(title = title) +
  theme_classic() +
  theme(axis.text = element_text(size = 14),
        axis.title = element_text(face = "bold", size = 16),
        title = element_text(face = "bold", size = 20))
 
}

Using the function

The customizations are saved in the function, simplifying the code and reducing the need for copying and pasting.

plot_mtcars(mpg, vs, "mpg vs vs")

Sleep dataset

glimpse(sleep)
Rows: 20
Columns: 3
$ extra <dbl> 0.7, -1.6, -0.2, -1.2, -0.1, 3.4, 3.7, 0.8, 0.0, 2.0, …
$ group <fct> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, …
$ ID    <fct> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8,…

Simple plot

sleep %>% 
  group_by(group) %>% 
  summarise(mean_extra_hrs = mean(extra), na.rm = T) %>% 
  ggplot(aes(group, mean_extra_hrs)) +
  geom_boxplot(aes(fill = group)) +
  geom_text(aes(label = mean_extra_hrs), vjust = -1) +
  theme_classic() +  
  labs(title = "Plot of mean_extra_hrs against group") +
  theme(axis.text = element_text(size = 14),
        axis.title = element_text(face = "bold", size = 16),
        title = element_text(face = "bold", size = 18))

Function

plot_boxplot_group_comparison <- function(data_df, var_x, var_y) {
  
  {{data_df}} %>% 
  group_by({{var_x}}) %>% 
  summarise(mean = mean({{var_y}}), na.rm = T) %>% 
  ggplot(aes({{var_x}}, mean)) +
  geom_boxplot(aes(fill = {{var_x}})) +
      geom_text(aes(label = mean), vjust = -1) +
    labs(title = 
           paste("Plot of ", as_label( enquo(var_y)), "against ", as_label(enquo(var_x))),
         y = paste("mean_", as_label(enquo(var_y)))) +
  theme_classic() +  
  theme(axis.text = element_text(size = 14),
        axis.title = element_text(face = "bold", size = 16),
        title = element_text(face = "bold", size = 20))
  
}


# using the function  
plot_boxplot_group_comparison(sleep, group, extra)

Using it on another dataset

# iris dataset

plot_boxplot_group_comparison(iris, Species, Petal.Width)

Thoughts

I really like this! Coding can be miminalist and functional at the same time.

References:

Citation

For attribution, please cite this work as

lruolin (2021, Oct. 4). pRactice corner: Easy plots with ggplot2. Retrieved from https://lruolin.github.io/myBlog/posts/20211006 Easy plots with ggplot2/

BibTeX citation

@misc{lruolin2021easy,
  author = {lruolin, },
  title = {pRactice corner: Easy plots with ggplot2},
  url = {https://lruolin.github.io/myBlog/posts/20211006 Easy plots with ggplot2/},
  year = {2021}
}